package com.focus.xyz.union.cases.frontend;

import com.focus.xyz.core.common.annotation.Task;
import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.common.listeners.TC;
import com.focus.xyz.core.common.webdriver.DriverFactory;
import com.focus.xyz.core.utils.Constants;
import com.focus.xyz.core.utils.ExcelUtils;
import com.focus.xyz.core.utils.ProcessUtil;
import com.focus.xyz.core.utils.prop.ConfigPropInfo;
import com.focus.xyz.union.action.KeyWordAction;
import com.focus.xyz.union.pages.backend.BackMenuList;
import com.focus.xyz.union.pages.backend.tables.MemTable;
import com.focus.xyz.union.pages.frontend.BaseInfoPage;
import com.focus.xyz.union.pages.frontend.FrontMenuList;
import com.focus.xyz.union.pages.frontend.MainPage;
import com.focus.xyz.union.pages.frontend.RegisterPage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import org.testng.annotations.*;

import java.io.IOException;

/**
 * Created by chenmeng on 2016/5/26.
 */
public class AcctPrepareTest extends TC{

    /*
    * 账号注册，后台账号审核，基础信息设置（选择省份、城市），账号充值
    *
    * */

    private WebDriver driver;
    private KeyWordAction action;


    @Parameters({"nodeUrl","browser","memName","memPass"})
    @BeforeMethod(alwaysRun = true)
    public void setUp(String nodeUrl,String browser,String memName,String memPass) throws Exception {
        driver = DriverFactory.getDriver(nodeUrl).getBrowser(browser);
        super.init(driver);
        action = new KeyWordAction(driver);
    }


    @Parameters({"browser"})
    @AfterMethod(alwaysRun = true)
    public void tearDown(String browser) {
        try{
            super.quit();
            driver.quit();
        }catch (Exception e){
            ProcessUtil.closeBrowser(browser);
        }finally {

        }
    }


    @DataProvider(name = "getRegistData")
    public Object[][] getRegistData(){
        String[][] data = null;
        try {
            ExcelUtils eu = new ExcelUtils();
            eu = eu.RestoreSettings();//还原设定
            eu.setStartReadPos(1);
            eu.setOnlyReadOneSheet(true);
            eu.setExcelPath(ConfigPropInfo.TEST_DATA_DIR + ConfigPropInfo.TEST_UNION_DATA_FILE);
            eu.setSelectedSheetName("regist");
            data = eu.readExcelArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return data;
    }

    @Task(taskName = {"RESET_LOGIN_ACCT.sql"})
    @Test(dataProvider = "getRegistData",description = "清除账号并重新注册账号",enabled = false)
    public void LOGIN_INFO_RESET(String memRole,String roleType,String userName,String email,String pass,String name,String bill,String idNo) throws Exception{
        action.open("http://union.xyz.cn/");
        //点击注册链接
        MainPage loginPage = new MainPage(driver,action);
        loginPage.clickRegisterLink();
        //完成注册
        RegisterPage registerPage = new RegisterPage(driver,action);
        registerPage.register(memRole,roleType,userName,email,pass,name,bill,idNo);
        action.pause(5000l);
        action.assertStringTrue(userName);
        action.assertStringTrue("您好！");
    }


    @Parameters({"memName","memPass"})
    @Test(description = "完善用户基本信息",priority = 0)
    public void FILL_BASE_INFO(String memName,String memPass) throws Exception{
        fillBaseInfo(memName,memPass);
    }

    public void fillBaseInfo(String memName,String memPass) throws Exception{
        action.LoginFront(memName,memPass);
        FrontMenuList frontMenuList = new FrontMenuList(driver,action);
        frontMenuList.openMenu(Constants.U_F_INFO_MENU);
        BaseInfoPage baseInfoPage = new BaseInfoPage(driver,action);
        baseInfoPage.fillBaseInfo("6666555","江苏省","南京市","大桥北路1号","焦点科技","123087");
        action.pause(1000);
        action.LogoutFront();
        action.DelUnionFrontCookie();
    }

    @Test(dataProvider = "memData")
    public void BATCH_FILL_BASE_INFO(String memName,String memPass) throws Exception{
        fillBaseInfo(memName,memPass);
    }

    @Test(description = "审核会员",dataProvider = "memData",priority = 1)
    public void APPROVAL_MEM(String memName,String memPass) throws Exception{
        action.LoginBack("xiapingping","ins1234");
        BackMenuList menuList = new BackMenuList(driver,action);
        menuList.openExpandMenu("渠道会员管理","查询渠道会员");
        MemTable memTable = new MemTable(driver,action);
        memTable.search(memName);
        action.pause(1000l);
        memTable.openLink(memName,"审核");
        action.pause(2000l);
        memTable.submit("Auto-审核通过");
    }

    @DataProvider(name = "memData")
    public Object[][] getMemData() throws Exception{
        return new String[][]{{"cimon001","ins1234"},{"cimon006","ins1234"},{"cimon007","ins1234"},{"cimon008","ins1234"},{"cimon009","ins1234"},{"cimon010","ins1234"},{"cimon011","ins1234"}};
    }


}
